/*
Content: Figure 2, Tables 1,2,3, E.1, E.2
Paper: "Immigrants Return Intentions and Labor Market Behavior when the Home Country is Unsafe"
Authors: Jacopo Bassetto, Teresa Freitas-Monteiro
*/

		
********************************************************************************
**# LOAD DATASET FOR THE MAIN ANALYSIS WITH GSOEP 
********************************************************************************

use "$final/finaldata_terrorsoep_light_final.dta", clear

	
version 16 

********************************************************************************
**# FIGURE 2
********************************************************************************

* Make distance positive for coef plot
foreach var in m5y	m4y m3y   {
cap drop posdistterror_`var'_p90
g posdistterror_`var'_p90=distanceterror_`var'_p90 + 3
}

* Plot
foreach var in	  m3y   {
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_`var'_p90,-3,3)"
global controls "i.soep_corigin_id#i.syear i.syear#i.month i.bula"  	
					
qui eststo event1: reghdfe remain_ger_per ib2.posdistterror_`var'_p90 if $select_reg_M  & sample_terror_`var'==1 & distanceterror_`var'_p90!=0  , vce(cluster clusterterror_`var'_p90) absorb($controls)

coefplot event1, keep(*.posdistterror_`var'_p90) vertical baselevel omitted ///
yline(0, lpattern(solid) lcolor(cranberry)) xline(3.5, lpattern(solid) lcolor(cranberry)) xlabel(1 "-3" 2 "-2" 3 "-1" 4 "1" 5 "2" 6 "3", nogri labsize(medsmall)) ///
ytitle("`l`out''", margin(medsmall))  xtitle("Months to event", margin(medsmall))  ///
 yscale(r(-0.20(0.10)0.30)) ylabel(-0.20(0.10)0.30, nogrid labsize(medsmall)) ///
graphregion(color(white)) ciopts(color(navy)) mcolor(navy) xsize(5.5) ysize(4)

graph save "Graph" "$graph/fig2_final.gph", replace
graph export "$graph/fig2_final.pdf", as(pdf) replace
graph export "$graph/fig2_final.eps", as(eps) replace

}

					
********************************************************************************
**# TABLE 1
********************************************************************************


eststo clear
local i=1				
foreach var in  m3y   {

global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_`var'_p90,-3,3) & distanceterror_`var'_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.syear#i.month" 	

qui  eststo m`i':  reghdfe remain_ger_per postterror_`var'_p90  if $select_reg_M & sample_terror_`var'==1, vce(cluster clusterterror_`var'_p90) absorb($controls)
estadd scalar nobs = e(N)
qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "No"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)
local i=`i'+1

qui  eststo m`i':  reghdfe remain_ger_per postterror_`var'_p90  i.sex c.age c.ysm##c.ysm  if $select_reg_M & sample_terror_`var'==1 , vce(cluster clusterterror_`var'_p90) absorb($controls)
estadd scalar nobs = e(N)
 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Some"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)
local i=`i'+1

qui  eststo m`i': reghdfe remain_ger_per postterror_`var'_p90  i.sex c.age c.ysm##c.ysm i.maritstat  i.child  if $select_reg_M & sample_terror_`var'==1 , vce(cluster clusterterror_`var'_p90) absorb($controls)
estadd scalar nobs = e(N)
qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Some"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)
local i=`i'+1

qui  eststo m`i':  reghdfe remain_ger_per postterror_`var'_p90  i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child  if $select_reg_M & sample_terror_`var'==1 , vce(cluster clusterterror_`var'_p90) absorb($controls)
estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)
local i=`i'+1
}


			
esttab m* using "$table/table1_final.tex", star(* 0.10 ** 0.05 *** 0.01) replace b(3) se(3) drop(*sex age *ysm *maritstat *educ_aftm2 *child _cons) nocons  nogaps nonotes noobs nomtitle mgroups("Higher than average of last 3 years", ///
            pattern(1 0 0 0) ///
            prefix(\multicolumn{@span}{c}{) suffix(}) ///
            span erepeat(\cmidrule(lr){@span})) varlabel(postterror_m3y_p90 "Post-Terror")  scalars( "nobs Observations" "ymean Mean intention to stay"  "orig_y Origin country x Year FE" "date Month FE x Year FE" "orig_m Origin country x Month FE" "state State of Residency FE" "indiv Indiv. Controls") sfmt(%9.0fc %9.3f %1s %1s %1s %1s %1s)

					
********************************************************************************
**# TABLE 2
********************************************************************************

eststo clear
local i=1	
foreach var in m3y  {
	global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_`var'_p90,-3,3) & distanceterror_`var'_p90!= 0"
	global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear" 	
	
	cap drop nkill_event
	g nkill_event= terror`var'p90_event10k 
qui eststo n`i': reghdfe remain_ger_per 1.postterror_`var'_p90#i.nkill_event    i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 if $select_reg_M & sample_terror_`var'==1 , vce(cluster clusterterror_`var'_p90) absorb($controls)
 estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)
local i=`i'+1

cap drop nkill_event
	g nkill_event= terror`var'p90_event10k 
qui eststo n`i': reghdfe remain_ger_per 1.postterror_`var'_p90#i.nkill_event   i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_terror_`var'==1 , vce(cluster clusterterror_`var'_p90) absorb($controls)
 estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)
local i=`i'+1


cap drop nkill_event
	g nkill_event= terror`var'p90_event10k 
qui eststo n`i':  reghdfe remain_ger_per 1.postterror_`var'_p90#i.nkill_event   i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M & sample_terror_`var'==1, vce(cluster clusterterror_`var'_p90) absorb($controls)
 estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)
local i=`i'+1
		
}


esttab n* using "$table/table2_final.tex", star(* 0.10 ** 0.05 *** 0.01) replace b(3) se(3) keep(1.postterror_m3y_p90*) nocons  nogaps nonotes noobs mtitle("k=10" "k=30" "k=50") mgroups("Higher than average of last 3 years", ///
            pattern(1 0 0) ///
            prefix(\multicolumn{@span}{c}{) suffix(}) ///
            span erepeat(\cmidrule(lr){@span})) varlabel(1.postterror_m3y_p90#0.nkill_event "Post-Terror" 1.postterror_m3y_p90#1.nkill_event "Post-Terror × (k or > than killed)")  scalars( "nobs Observations" "ymean Mean intention to stay"  "orig_y Origin country x Year FE" "date Month FE x Year FE" "orig_m Origin country x Month FE" "state State of Residency FE" "indiv Indiv. Controls") sfmt(%9.0fc %9.3f %1s %1s %1s %1s %1s)




********************************************************************************
**# TABLE 3
********************************************************************************

global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear i.soep_corigin_id#i.syear"
label var postterror_m3y_p90 "Post-Terror"
 				

sum PolStab_rank mean_PolStab_rank_m3y if $select_reg_M    & sample_terror_m3y==1, d
sum eventmean_terror_m1y eventmean_terror_m3y if $select_reg_M    & sample_terror_m3y==1, d


eststo clear 
local i=1
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear i.soep_corigin_id#i.syear" 				

foreach x in rank_25_50_75_m1y rank_25_50_75_m3y {
	cap drop rank_index
	clonevar rank_index=`x'
eststo reg`i': reghdfe remain_ger_per c.postterror_m3y_p90#i.rank_index i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M    & sample_terror_m3y==1, vce(cluster clusterterror_m3y_p90) absorb($controls)  estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)
local i=`i'+1
}
esttab reg*  using "$table/table3_final.tex", star(* 0.10 ** 0.05 *** 0.01) replace b(3) se(3) keep(c.postterror_m3y_p90#1.rank_index c.postterror_m3y_p90#2.rank_index c.postterror_m3y_p90#3.rank_index  ) label nocons  nogaps nonotes noobs mtitle("Previous year" "Mean previous 3 years") mgroups("Political stability index", ///
            pattern(1 0 ) ///
            prefix(\multicolumn{@span}{c}{) suffix(}) ///
            span erepeat(\cmidrule(lr){@span})) varlabel(c.postterror_m3y_p90#1.rank_index "Post-Terror × Pol. Stab. <=25 " c.postterror_m3y_p90#2.rank_index "Post-Terror × Pol. Stab. [25-75]" c.postterror_m3y_p90#3.rank_index "Post-Terror × Pol. Stab. > 75")  scalars( "nobs Observations"  "orig_y Origin country x Year FE" "date Month FE x Year FE" "orig_m Origin country x Month FE" "state State of Residency FE" "indiv Indiv. Controls") sfmt(%9.0fc %9.3f %1s %1s %1s %1s %1s)




********************************************************************************
**# TABLE E.1
********************************************************************************
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear i.soep_corigin_id#i.syear"
reghdfe remain_ger_per c.postterror_m3y_p90 i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M    & sample_terror_m3y==1, vce(cluster clusterterror_m3y_p90) absorb($controls)
g sample_fin=e(sample)

eststo clear 
estpost sum PolStab_rank mean_PolStab_rank_m3y  eventmean_terror_m1y eventmean_terror_m3y if  sample_fin==1, d listwise
esttab using "$table/stats_stability_rev1.tex", cells("mean(fmt(3)) p5(fmt(3)) p10(fmt(3)) p25(fmt(3)) p50(fmt(3)) p75(fmt(3)) p90(fmt(3)) p95(fmt(3))") nomtitle nonumber label replace


********************************************************************************
**# TABLE E.2
********************************************************************************

eststo clear 
local i=1
global select_reg_M "!inlist(soep_corigin_id,.,1) & inrange(distanceterror_m3y_p90,-3,3) & distanceterror_m3y_p90!= 0"
global controls "i.soep_corigin_id#i.syear  i.bula i.month#i.syear i.soep_corigin_id#i.syear" 				

foreach x in event_scale_nr_m1y_v1 event_scale_nr_v1  {
		cap drop rank_index
	clonevar rank_index=`x'
eststo reg`i': reghdfe remain_ger_per c.postterror_m3y_p90#i.rank_index i.sex c.age c.ysm##c.ysm i.maritstat i.educ_aftm2 i.child if $select_reg_M    & sample_terror_m3y==1, vce(cluster clusterterror_m3y_p90) absorb($controls)
estadd scalar nobs = e(N)
	 qui estadd local orig_y "Yes"
qui estadd local date "Yes"
qui estadd local orig_m "Yes"
qui estadd local state "Yes"
qui estadd local indiv "Yes"
sum	 remain_ger_per if e(sample)==1 & postterror_m3y_p90==0
estadd scalar ymean = r(mean)
local i=`i'+1
}

esttab reg*  using "$table/tableE2_final.tex", star(* 0.10 ** 0.05 *** 0.01) replace b(3) se(3) keep(c.postterror_m3y_p90#1.rank_index c.postterror_m3y_p90#2.rank_index c.postterror_m3y_p90#3.rank_index  ) label nocons  nogaps nonotes noobs mtitle("Previous year" "Mean previous 3 years") mgroups("Mean monthly terror", ///
            pattern(1 0 ) ///
            prefix(\multicolumn{@span}{c}{) suffix(}) ///
            span erepeat(\cmidrule(lr){@span})) varlabel(c.postterror_m3y_p90#1.rank_index "Post-Terror × >12 attacks month " c.postterror_m3y_p90#2.rank_index "Post-Terror × [0-12] attacks month" c.postterror_m3y_p90#3.rank_index "Post-Terror × 0 attacks month")  scalars( "nobs Observations"  "orig_y Origin country x Year FE" "date Month FE x Year FE" "orig_m Origin country x Month FE" "state State of Residency FE" "indiv Indiv. Controls") sfmt(%9.0fc %9.3f %1s %1s %1s %1s %1s)

